package com.shopping.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckUtil {

    public static boolean checkPhone(String phone) {
        String regExp = "^[0-9]{11}$";
        Pattern pattern = Pattern.compile(regExp);
        Matcher matcher = pattern.matcher(phone);
        if(matcher.matches()){
            return true;
        }
        return false;
    }

    /**
     * 地区代码
     * @return Hashtable
     */
    private static Hashtable GetAreaCode() {
        Hashtable hashtable = new Hashtable();
        hashtable.put("11", "北京");
        hashtable.put("12", "天津");
        hashtable.put("13", "河北");
        hashtable.put("14", "山西");
        hashtable.put("15", "内蒙古");
        hashtable.put("21", "辽宁");
        hashtable.put("22", "吉林");
        hashtable.put("23", "黑龙江");
        hashtable.put("31", "上海");
        hashtable.put("32", "江苏");
        hashtable.put("33", "浙江");
        hashtable.put("34", "安徽");
        hashtable.put("35", "福建");
        hashtable.put("36", "江西");
        hashtable.put("37", "山东");
        hashtable.put("41", "河南");
        hashtable.put("42", "湖北");
        hashtable.put("43", "湖南");
        hashtable.put("44", "广东");
        hashtable.put("45", "广西");
        hashtable.put("46", "海南");
        hashtable.put("50", "重庆");
        hashtable.put("51", "四川");
        hashtable.put("52", "贵州");
        hashtable.put("53", "云南");
        hashtable.put("54", "西藏");
        hashtable.put("61", "陕西");
        hashtable.put("62", "甘肃");
        hashtable.put("63", "青海");
        hashtable.put("64", "宁夏");
        hashtable.put("65", "新疆");
        hashtable.put("71", "台湾");
        hashtable.put("81", "香港");
        hashtable.put("82", "澳门");
        hashtable.put("91", "国外");
        return hashtable;
    }

    /**
     * 判断字符串是否为数字
     * @param str
     * @return
     */
    private static boolean isNumber(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (isNum.matches()) {
            return true;
        } else {
            return false;
        }
    }

    private static boolean isBirthday(String year, String month, String day){
        String dateStr = year + "-" + month + "-" + day;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            dateFormat.setLenient(false);
            date = dateFormat.parse(dateStr);
        } catch (ParseException e) {
            return false;
        }

        Date nowDate = new Date();
        String nowDateStr = dateFormat.format(nowDate);
        int yearNum = Integer.parseInt(year);
        int monthNum = Integer.parseInt(month);
        int dayNum = Integer.parseInt(day);
        int nowYearNum = Integer.parseInt(nowDateStr.substring(0, 4));
        int nowMonthNum = Integer.parseInt(nowDateStr.substring(6, 7));
        int nowDayNum = Integer.parseInt(nowDateStr.substring(9, 10));
        if(nowYearNum - yearNum < 0 || nowYearNum - yearNum > 100){
            return false;
        }else if(nowYearNum - yearNum == 0){
            if(nowMonthNum - monthNum < 0){
                return false;
            }else if(nowMonthNum - monthNum == 0){
                if(nowDayNum - dayNum < 0){
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 判断该身份证号是否合理
     * @param idNumber
     * @return
     */
    public static boolean checkIdCard(String idNumber){
        // 加权因子
        int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
        // 第18位校检码
        char verifyCode[] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
        String num = "";

        // 判断长度
        if(idNumber.length() == 18){
            num = idNumber.substring(0, 17);
        }else if(idNumber.length() == 15){
            num = idNumber.substring(0, 6) + "19" + idNumber.substring(6, 15);
        }else {
            return false;
        }

        // 判断前 17 位是否全为数字
        if(!isNumber(num)){
            return false;
        }

        // 判断生日是否合理
        String strYear = idNumber.substring(6, 10);// 年份
        String strMonth = idNumber.substring(10, 12);// 月份
        String strDay = idNumber.substring(12, 14);// 月份
        if(!isBirthday(strYear, strMonth, strDay)){
            return false;
        }

        // 判断最后一位校验位是否正确
        int checkNum = 0;
        for(int i = 0; i < 17; i++){
            checkNum += (power[i] * Integer.parseInt("" + num.charAt(i)));
        }
        checkNum %= 11;
        if(verifyCode[checkNum] != idNumber.charAt(17)){
            return false;
        }
        return true;
    }
}
